package com.etc.dao.impl;

import java.util.List;

import com.etc.dao.AnsMessageDao;
import com.etc.dao.BaseDao;
import com.etc.entity.AnsMessage;
import com.etc.entity.Message;
import com.etc.entity.PageCount;

public class AnsMessageDaoImpl extends BaseDao implements AnsMessageDao{

	@Override
	public int answerMessage(AnsMessage a) {
		String	sql="insert into ansmessages_info(id,username,title,type,content,date,status,ansid) values(?,?,?,?,?,CURRENT_TIMESTAMP(),'正常',?)";
		Object[] args= {a.getId(),a.getUsername(),a.getTitle(),a.getType(),a.getContent(),a.getAnsid()};
		return this.myExecuteUpdate(sql, args);
	}

	@Override
	public List<AnsMessage> findTitleAnsMessage(int ansid) {
		// TODO Auto-generated method stub
		String sql="select * from ansmessages_info where ansid=? ORDER BY date desc";
		Object[] args= {ansid};
		return this.listResult(AnsMessage.class, sql, args);
	}

	@Override
	public int deleteAnsAnsMessage(int ansid) {
		// TODO Auto-generated method stub
		String sql = "delete from ansAnswers_info where ansid=?";
		Object[] args= {ansid};
		return this.myExecuteUpdate(sql, args);
	}

	@Override
	public int deleteAnsMessage(int id) {
		// TODO Auto-generated method stub
		String sql="delete from ansmessages_info where id=?";
		Object[] args= {id};
		return this.myExecuteUpdate(sql, args);
	}

	@Override
	public int closeAnsMessage(int id) {
		// TODO Auto-generated method stub
		String sql="update ansmessages_info set status=? where id=?";
		Object[] args= {"封禁",id};
		return this.myExecuteUpdate(sql, args);
	}

	@Override
	public int openAnsMessage(int id) {
		// TODO Auto-generated method stub
		String sql="update ansmessages_info set status=? where id=?";
		Object[] args= {"正常",id};
		return this.myExecuteUpdate(sql, args);
	}

	@Override
	public List<AnsMessage> findUserAnsMessage(String username) {
		// TODO Auto-generated method stub
		if(username!=null) {
    	String sql="select * from ansmessages_info where username=? ORDER BY date desc";
    	Object[] args= {username};
		return this.listResult(AnsMessage.class, sql, args);
		}
		else {
			String sql="select * from ansmessages_info  ORDER BY date desc";
	    	Object[] args= {};
			return this.listResult(AnsMessage.class, sql, args);
		}
	}

	@Override
	public List<AnsMessage> searchAllAnsMessage() {
		// TODO Auto-generated method stub
		String sql="select * from ansmessages_info ORDER BY date desc";
		return this.listResult(AnsMessage.class, sql, null);
	}

	@Override
	public Long countAll() {
		// TODO Auto-generated method stub
		String sql="select count(1) as totalCount from ansmessages_info";
		Object[] args= {};
		return this.loadResult(PageCount.class, sql, args).getTotalCount();
	}

	@Override
	public List<AnsMessage> mySearch(String username, String search,int pageIndex,int pageSize) {
		// TODO Auto-generated method stub
		if(username!=null && search!=null) {
		String sql="select * from ansmessages_info where username=? and content like CONCAT('%',?,'%') limit ?,?";
		Object[] args = {username,search,(pageIndex-1)*pageSize,pageSize};
		return this.listResult(AnsMessage.class, sql, args);
		}		
		if(username.equals("") && search!=null){ 
         String sql="select * from ansmessages_info where content like CONCAT('%',?,'%') limit ?,?"; 
         Object[] args = {search,(pageIndex-1)*pageSize,pageSize}; 
         return this.listResult(AnsMessage.class, sql, args); 
         }
		 
		if(username!=null && search.equals("")){
			String sql="select * from ansmessages_info where username=? limit ?,?";
			Object[] args = {username,(pageIndex-1)*pageSize,pageSize};
			return this.listResult(AnsMessage.class, sql, args);
		}
		if(username.equals("") && search.equals("")) {
			String sql="select * from ansmessages_info limit ?,?";
			Object[] args = {(pageIndex-1)*pageSize,pageSize};
			return this.listResult(AnsMessage.class, sql, args);
		}
		return null;
	}

	@Override
	public Long countAllUser(String username) {
		// TODO Auto-generated method stub
		String sql="select count(1) as totalCount from ansmessages_info where username=?";
		Object[] args = {username};
		return this.loadResult(PageCount.class, sql, args).getTotalCount();
	}

	@Override
	public List<AnsMessage> myAns(String username, int pageIndex, int pageSize) {
		// TODO Auto-generated method stub
		if(username!=null) {
			String sql="select * from ansmessages_info where username=? limit ?,?";
			Object[] args= {username,(pageIndex-1)*pageSize,pageSize};
			return this.listResult(AnsMessage.class, sql, args);
			}
			else {
				String sql="select * from ansmessages_info limit ?,?";
				Object[] args= {(pageIndex-1)*pageSize,pageSize};
				return this.listResult(AnsMessage.class, sql, args);
			}
	}


}
